package library;

import java.io.Serializable;
import java.util.Collections;
import java.util.Iterator;
import java.util.Observable;
import java.util.Vector;

import javax.swing.table.AbstractTableModel;

public class LibraryModel
extends Observable
implements Serializable
{
	private static final long serialVersionUID = 2273709229512606239L;
	
	private Vector<Lending> lendings = null;
	private Vector<Book> books = null;
	private Vector<Student> students = null;
	
	private BookModel bookModel;
	private StudentModel studentModel;
	private LendingModel lendingModel;
	
	public LibraryModel(Book[] books, Student[] students)
	{
		lendings = new Vector<Lending>();
		
		this.books = new Vector<Book>();
		for (Book b : books)
			this.books.add(b);
		Collections.sort(this.books);
		
		this.students = new Vector<Student>();
		for (Student s : students)
			this.students.add(s);
		Collections.sort(this.students);
		
		bookModel = new BookModel(this.books);
		studentModel = new StudentModel(this.students);
		lendingModel = new LendingModel(this.lendings);
		
	}
	
	public Vector<Student> getStudents()
	{
		return this.students;
	}
	
	public Vector<Book> getBooks()
	{
		return this.books;
	}
	
	public Vector<Lending> getLendings()
	{
		return this.lendings;
	}
	
	public void returnBook(int idBook, int idStudent)
	{
		Book b = this.books.get(idBook);
		Student s = this.students.get(idStudent);
		
		returnBook(b, s);
	}
	
	public void returnBook(Book b, Student s)
	{
		b.returnBook();
		
		Iterator<Lending> iter = this.lendings.iterator();
		
		while (iter.hasNext())
		{
			Lending l = iter.next();
			if (l.getBook().equals(b) && l.getStudent().equals(s))
			{
				iter.remove();
			}
		}
		
		this.lendingModel.reBuild(this.lendings);
		this.bookModel.reBuild(this.books);
	}
	
	public void lendBook(int idBook, int idStudent) throws LendingException
	{
		Book b = this.books.elementAt(idBook);
		Student s = this.students.elementAt(idStudent);
		lendBook(b, s);
	}
	
	public void lendBook(Book b, Student s) throws LendingException
	{
		Lending l = this.books.get(this.books.indexOf(b)).lendBook(s);
		this.lendings.add(l);
		
		this.bookModel.reBuild(this.books);
		this.lendingModel.add(l);
	}
	
	public void addBook(Book b)
	{
		// TODO properly
		this.books.add(b);
	}
	
	public void addStudent(Student s)
	{
		//TODO properly
		this.students.add(s);
	}
	
	public void addStudent(String name)
	{
		int newId = this.students.lastElement().getId() + 1;
		Student newStudent = new Student(newId, name);
		
		this.students.add(newStudent);
		this.studentModel.add(newStudent);
		
		this.setChanged();
		this.notifyObservers();
	}

	public void addBook(String isbn, String title, String author)
	{
		int newId = this.books.lastElement().getId() + 1;
		Book newBook = new Book(newId, isbn, title, author);
		this.books.add(newBook);
		this.bookModel.add(newBook);
		
		this.setChanged();
		this.notifyObservers();
	}
	
	public AbstractTableModel getBookModel()
	{
		return this.bookModel;
	}
	
	public AbstractTableModel getStudentModel()
	{
		return this.studentModel;
	}
	
	public AbstractTableModel getLendingModel()
	{
		return this.lendingModel;
	}
}

